package com.yksj.healthtalk.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.ui.R;

public class OrderForGoodAdapter extends BaseAdapter {
	JSONArray mArray = new JSONArray();
	private Context mContext;
	private LayoutInflater mInflater;

	
	public OrderForGoodAdapter(Context context) {
		super();
		this.mContext=context;
		mInflater = LayoutInflater.from(mContext);
	}

	public void onDataChange(JSONArray array){
		this.mArray = array;
		notifyDataSetChanged();
	}
	
	public void onClearData(){
		this.mArray.clear();
		notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		return mArray.size();
	}

	@Override
	public JSONObject getItem(int position) {
		return mArray.getJSONObject(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}


	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		ViewHolder holder;
		JSONObject jsonObject = getItem(arg0);
		if(arg1==null){
			holder=new ViewHolder();
			arg1=mInflater.inflate(R.layout.orderforgoods_item,null);	
			holder.orider_id= (TextView) arg1.findViewById(R.id.order_id);
			holder.orider_money= (TextView) arg1.findViewById(R.id.order_money);
			holder.orider_time= (TextView) arg1.findViewById(R.id.order_time);
			holder.orider_entity= (TextView) arg1.findViewById(R.id.order_entity);
			holder.orider_status= (TextView) arg1.findViewById(R.id.order_status);
			arg1.setTag(holder);
		}else{
			holder=(ViewHolder) arg1.getTag();
		}
		holder.orider_id.setText(jsonObject.getString("ORDER_ID"));
		holder.orider_money.setText(jsonObject.getString("MERCHANT_REBATES_GOLD"));
		holder.orider_time.setText(jsonObject.getString("ORDER_TIME"));
		
		JSONArray jsonArray = jsonObject.getJSONArray("goods");
		StringBuffer buffer=new StringBuffer();
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject object= (JSONObject) jsonArray.get(i);
			buffer.append(object.getString("GOODS_NAME"));
		}
		holder.orider_entity.setText(buffer.toString());
		
		holder.orider_status.setText(jsonObject.getString("ORDER_STATE"));
		return arg1;
	}
	class ViewHolder{
		private TextView orider_id;
		private TextView orider_money;
		private TextView orider_time;
		private TextView orider_entity;
		private TextView orider_status;
	}
}
